package com.ksd.modules.download.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.download.entity.AdminDownloadsComment;
import com.ksd.modules.download.mapper.AdminDownloadsCommentMapper;
import com.ksd.modules.download.po.AdminDownloadsCommentSavePO;
import com.ksd.modules.download.po.AdminDownloadsCommentSearchPO;
import com.ksd.modules.download.service.IAdminDownloadsCommentReplyService;
import com.ksd.modules.download.service.IAdminDownloadsCommentService;
import com.ksd.modules.download.vo.AdminDownloadsCommentReplyVo;
import com.ksd.modules.download.vo.AdminDownloadsCommentVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminDownloadsCommentServiceImpl extends BaseServiceImpl<AdminDownloadsCommentMapper, AdminDownloadsComment> implements IAdminDownloadsCommentService {

    @Autowired
    private IAdminDownloadsCommentReplyService adminDownloadsCommentReplyService;

    /**
     * 查询列表
     * @param adminDownloadsCommentSearchPO
     * @return
     */
    public Page<AdminDownloadsCommentVo> findAdminDownloadsComments(AdminDownloadsCommentSearchPO adminDownloadsCommentSearchPO){
        Page<AdminDownloadsComment> page = new Page<>(adminDownloadsCommentSearchPO.getPageNo(),adminDownloadsCommentSearchPO.getPageSize());
        LambdaQueryWrapper<AdminDownloadsComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminDownloadsComment::getSystemId,SystemIdThrealLocal.get().getSystemId());
        // 资源id
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsCommentSearchPO.getOpid()),AdminDownloadsComment::getOpid,adminDownloadsCommentSearchPO.getOpid());
        // 用户唯一标识
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminDownloadsCommentSearchPO.getUuid()),AdminDownloadsComment::getUuid,adminDownloadsCommentSearchPO.getUuid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminDownloadsCommentSearchPO.getStatus()),AdminDownloadsComment::getStatus,adminDownloadsCommentSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminDownloadsCommentSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminDownloadsComment::getUsername, adminDownloadsCommentSearchPO.getKeyword())
                    .or().like(AdminDownloadsComment::getUuid, adminDownloadsCommentSearchPO.getKeyword())
                    .or().like(AdminDownloadsComment::getContent, adminDownloadsCommentSearchPO.getKeyword())
                    .or().like(AdminDownloadsComment::getTitle, adminDownloadsCommentSearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminDownloadsComment::getCreateTime);
        Page<AdminDownloadsComment> adminDownloadsPage = this.page(page, lambdaQueryWrapper);
        Page<AdminDownloadsCommentVo> adminDownloadsVoPage = tranferPageBo(adminDownloadsPage, AdminDownloadsCommentVo.class);
        List<AdminDownloadsCommentVo> records = adminDownloadsVoPage.getRecords();
        // 查询所有的第一页的子回复
        if(CollectionUtils.isNotEmpty(records)){
            for (AdminDownloadsCommentVo adminDownloadsCommentVo : records) {
                AdminDownloadsCommentSearchPO adminDownloadsCommentSearchPO2 = new AdminDownloadsCommentSearchPO();
                adminDownloadsCommentSearchPO2.setPageNo(1);
                adminDownloadsCommentSearchPO2.setPageSize(3);
                adminDownloadsCommentSearchPO2.setParentId(adminDownloadsCommentVo.getId());
                Page<AdminDownloadsCommentReplyVo> childrenReplysPage = adminDownloadsCommentReplyService.findAdminDownloadsCommentReplys(adminDownloadsCommentSearchPO2);
                adminDownloadsCommentVo.setReplyNum(childrenReplysPage.getTotal());
                adminDownloadsCommentVo.setChildren(childrenReplysPage);
            }
        }
        return adminDownloadsVoPage;
    }

    /**
     * 保存和更新
     * @param adminDownloadsCommentSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminDownloadsCommentVo saveUpdateAdminDownloadsComment(AdminDownloadsCommentSavePO adminDownloadsCommentSavePO){
        AdminDownloadsComment adminDownloads = tranferBo(adminDownloadsCommentSavePO, AdminDownloadsComment.class);
        adminDownloads.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminDownloads.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminDownloads);
        AdminDownloadsCommentVo adminDownloadsVo = tranferBo(adminDownloads, AdminDownloadsCommentVo.class);
        return adminDownloadsVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminDownloadsCommentVo getAdminDownloadsCommentById(Long id){
        AdminDownloadsComment adminDownloads = this.getById(id);
        AdminDownloadsCommentVo adminDownloadsVo = tranferBo(adminDownloads, AdminDownloadsCommentVo.class);
        return adminDownloadsVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminDownloadsCommentById(Long id){
        boolean b = this.removeById(id);
        if(b){
            adminDownloadsCommentReplyService.removeByParentId(id);
        }
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminDownloadsComment(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        if(b){
            adminDownloadsCommentReplyService.batchRemoveByParentId(ids);
        }
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        if(b){
            adminDownloadsCommentReplyService.updateStatusByParentId(id,column,value);
        }
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        if(b){
            adminDownloadsCommentReplyService.updateStatusByParentId(id,column,value);
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminDownloadsComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b){
            adminDownloadsCommentReplyService.batchUpdateStatusByParentIds(ids,"status", StatusEnums.UN_STATUS.getCode());
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminDownloadsComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b){
            adminDownloadsCommentReplyService.batchUpdateStatusByParentIds(ids,"status", StatusEnums.STATUS.getCode());
        }
        return b;
    }
}
